package com.sky.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);

        //查询
        ShoppingCart cart = shoppingCartMapper
                .queryByDishIdOrSetmealIdWithUserId(
                BaseContext.getCurrentId(),
                shoppingCartDTO.getDishId(),
                shoppingCartDTO.getSetmealId(),
                shoppingCartDTO.getDishFlavor());

        //存在则更新数量
        if(!ObjectUtils.isEmpty(cart)){
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }else{
            //不存在则添加
            cart = new ShoppingCart();
            cart.setUserId(BaseContext.getCurrentId());
            cart.setNumber(1);
            cart.setCreateTime(LocalDateTime.now());
            if(!ObjectUtils.isEmpty(shoppingCartDTO.getSetmealId())) {
                Setmeal setmeal = setmealMapper.queryById(shoppingCartDTO.getSetmealId());
                cart.setSetmealId(setmeal.getId());
                cart.setName(setmeal.getName());
                cart.setImage(setmeal.getImage());
                cart.setAmount(setmeal.getPrice());
            }else{
                //获取菜品
                Dish dish = dishMapper.queryById(Math.toIntExact(shoppingCartDTO.getDishId()));
                cart.setDishId(dish.getId());
                cart.setName(dish.getName());
                cart.setImage(dish.getImage());
                cart.setAmount(dish.getPrice());
            }
            // 判断是否有口味
            if(!StringUtils.isEmpty(shoppingCartDTO.getDishFlavor())) cart.setDishFlavor(shoppingCartDTO.getDishFlavor());

            //添加到数据库
            shoppingCartMapper.insert(cart);
        }
    }


    /**
     * 查询购物车
     * @param userId
     * @return
     */
    @Override
    public List<ShoppingCart> listCart(Long userId) {
        List<ShoppingCart> result = shoppingCartMapper.queryByUserId(userId);
        return result;
    }

    /**
     * 清空购物车
     * @param userId
     */
    @Override
    public void clean(Long userId) {
        shoppingCartMapper.clean(userId);
    }

    /**
     * 减少购物车
     * @param shoppingCartDTO
     */
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //查询购物车
        ShoppingCart shoppingCart = shoppingCartMapper.queryByDishIdOrSetmealIdWithUserId(
                BaseContext.getCurrentId(),
                shoppingCartDTO.getDishId(),
                shoppingCartDTO.getSetmealId(),
                shoppingCartDTO.getDishFlavor());
        //获取数量并判断
        Integer number = shoppingCart.getNumber();
        if(number > 1) {
            //更新数量
            shoppingCart.setNumber(number - 1);
            shoppingCartMapper.update(shoppingCart);
        }else {
            //删除购物车
            shoppingCartMapper.delete(shoppingCart.getId());
        }
    }
}

/**
 * 添加购物车，此方法已被废弃，逻辑混杂不清晰，已被上面的方法替代
 * @param shoppingCartDTO
 */
    /*@Transactional
    @Override
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前是菜品还是套餐
        if (ObjectUtils.isEmpty(shoppingCartDTO.getSetmealId())) {
            // 菜品
            // 判断购物车中是否已经存在该菜品
            ShoppingCart shoppingCart = shoppingCartMapper.queryByDishIdWithUserId(BaseContext.getCurrentId(), shoppingCartDTO.getDishId());
            // 如果存在或者口味一样则更新数量
            if (!ObjectUtils.isEmpty(shoppingCart) ) {
                // 如果存在则更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                //更新到数据库
                shoppingCartMapper.update(shoppingCart);
            }else{
                // 如果不存在则添加
                shoppingCart = new ShoppingCart();
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                //获取菜品
                Dish dish = dishMapper.queryById(Math.toIntExact(shoppingCartDTO.getDishId()));
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
                // 判断是否有口味
                if(!StringUtils.isEmpty(shoppingCartDTO.getDishFlavor())) shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                //添加到数据库
                shoppingCartMapper.insert(shoppingCart);
            }
        } else if (!ObjectUtils.isEmpty(shoppingCartDTO.getSetmealId())){
            // 套餐
            // 判断购物车中是否已经存在该套餐
            ShoppingCart shoppingCart = shoppingCartMapper.queryBySetmealIdWithUserId(BaseContext.getCurrentId(), shoppingCartDTO.getSetmealId());
            // 如果存在或者口味一样则更新数量
            if (!ObjectUtils.isEmpty(shoppingCart)) {
                // 如果存在则更新数量
                shoppingCart.setNumber(shoppingCart.getNumber() + 1);
                //更新到数据库
                shoppingCartMapper.update(shoppingCart);
            }else{
                // 如果不存在则添加
                shoppingCart = new ShoppingCart();
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                //获取套餐
                Setmeal setmeal = setmealMapper.queryById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
                // 判断是否有口味
                if(!StringUtils.isEmpty(shoppingCartDTO.getDishFlavor())) shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                //添加到数据库
                shoppingCartMapper.insert(shoppingCart);
            }
        }

    }
*/
